package com.topica.database.task;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.topica.database.DataBaseHelper;
import com.topica.database.callback.OnGetListLectureListener;
import com.topica.object.LectureItem;
import com.topica.util.Util;

public class GetListLectureTask extends TopicaDBTask {
	private static final String TAG = "GetListLectureTask";
	private ArrayList<LectureItem> itemsTemp;
	private ArrayList<LectureItem> items;
	private OnGetListLectureListener listener;
	private String weekId;

	public GetListLectureTask(Context context, String weekId) {
		super(context);
		this.weekId = weekId;
		items = new ArrayList<LectureItem>();
	}

	@Override
	protected Integer doInBackground(Void... params) {
		DataBaseHelper myDbHelper = new DataBaseHelper(
				context.getApplicationContext());
		itemsTemp = myDbHelper.getListLectures(weekId);
		for(LectureItem item : itemsTemp){
			System.out.println("book: " + item.getPathBook() + " audio " + item.getPathAudio() + " video " + item.getPathVideo());
			System.out.println("has book " + Util.hasBook(item.getPathBook()) + " has audio " + Util.hasAudio(item.getPathAudio()) + " has video " + Util.hasVideo(item.getPathVideo()));
			item.setHasAudio(Util.hasAudio(item.getPathAudio()));
			item.setHasBook(Util.hasBook(item.getPathBook()));
			item.setHasVideo(Util.hasVideo(item.getPathVideo()));
			items.add(item);
		}
		myDbHelper.close();
		return OK;
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		if (result == OK) {
			listener.onGetListLectureListener(items);
		} else {
			Log.i(TAG, "Co loi trong qua trinh lay danh sach Lecture");
		}
	}

	public void setOnGetListLectureListener(OnGetListLectureListener listener) {
		this.listener = listener;
	}

}
